<?php
namespace app\api\controller;

class Robot extends Base
{
    //机器人

    //读取卡信息和用户信息
    public function cardOne()
    {
        $pas = $this->request->param();
        $serial = isset($pas['serial'])&&$pas['serial'] ? $pas['serial'] : '';
        if (!$serial) {
            return $this->toJson(-2, '卡ID必须的');
        }
        $res = $res = $this->obj()->cardOne($serial);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //开门
    public function doorOpen()
    {
        $pas = $this->request->param();
        //$imei = isset($pas['imei'])&&$pas['imei'] ? $pas['imei'] : '';
        $doorno = isset($pas['door_no'])&&$pas['door_no'] ? $pas['door_no'] : ''; //门编号
        $oper_type = isset($pas['oper_type'])&&$pas['oper_type'] ? $pas['oper_type'] : 'consume'; //操作 consume 消费； manage 管理
        if (!$doorno) {
            return $this->toJson(-2, '机器参数缺失');
        }
        $orderno = $this->orderno();
        $httpUrl = $this->domain.'/equipment/opendoor';
        $httpData = [
            'equipment_id' => $doorno,
            'type' => $oper_type,
            'order_sn' => $orderno,
        ];
        $httpData = $this->apiSign($httpData);
        $httpData = json_encode($httpData);
//        var_dump($httpUrl); echo '<br><br>';
//        var_dump($httpData); echo '<br><br>';
        $logid = $this->xykw()->orderAdd([
            'order_sn' => $orderno,
            'lly_url' => $httpUrl, 'lly_req' => $httpData,
            'door_info' => json_encode([
                'imei' => '', 'door_no' => $doorno,
            ]),
        ]); //订单日志
        $res = $this->toCurl($httpUrl, 1, $httpData);
        $this->xykw()->orderModify($logid, ['lly_back' => json_encode($res, JSON_UNESCAPED_UNICODE)]); //订单日志
//        ddd($res);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        $xykw = $res['data'];
//        ddd($xykw);
        if ($xykw['code'] != 200) {
            return $this->toJson(-2, $xykw['message']);
        }
        $res = $this->obj()->doorOpen($doorno);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}